package com.ptoc.carownerservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ptoc.carownerservice.entity.EduTeacher;
import com.ptoc.carownerservice.entity.vo.TeacherQuery;
import com.ptoc.carownerservice.service.EduTeacherService;
import com.ptoc.commonutils.resultutils.R;
import com.ptoc.servicebase.exceptionhandler.GuliException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2021-09-28
 */
@Api(description = "讲师管理")
@RestController
@RequestMapping("/eduservice/teacher")
@Slf4j
public class EduTeacherController {
    @Autowired
    private EduTeacherService teacherService;

    //所有讲师列表
    @ApiOperation(value = "所有讲师列表")
    @GetMapping("fiandAll")
    public R list() {
        List<EduTeacher> list = teacherService.list(null);
        try {
            int a = 10/0;
        }catch (Exception e){
            log.error("EduTeacherController 发生错误");
            throw new GuliException(20001,"执行了自定义 GuliException 的异常处理。。。");
        }
        return R.ok().data("iteams", list);
    }

    //逻辑删除讲师信息
    @ApiOperation(value = "逻辑删除讲师信息")
    @DeleteMapping("{id}")
    public R deleteTeacherById(@ApiParam(name = "id", value = "讲师ID", required = true) @PathVariable String id) {
        boolean b = teacherService.removeById(id);
        if (b) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    //分页查询讲师方法  current当前页  limit 每页当前记录数
    @ApiOperation(value = "分页查询讲师信息")
    @GetMapping("pageTeacher/{current}/{limit}")
    public R pageListTeacher(@PathVariable long current, @PathVariable long limit) {

        Page<EduTeacher> page = new Page<>(current, limit);
        IPage<EduTeacher> teacherIPage = teacherService.page(page, null);

        long total = teacherIPage.getTotal(); //总记录数
        List<EduTeacher> records = teacherIPage.getRecords(); //数据集合

        //第二种返回方式
//        Map map = new HashMap<>();
//        map.put("total", total);
//        map.put("rows", records);
//        return R.ok().data("data", map);

        return R.ok().data("total", total).data("rows", records);

    }

    //条件分页查询讲师方法  current当前页  limit 每页当前记录数
    @ApiOperation(value = "条件分页查询讲师信息")
    @PostMapping("pageListTeacherCondition/{current}/{limit}")
    public R pageListTeacherCondition(@PathVariable long current, @PathVariable long limit,
                                      @RequestBody(required = false) TeacherQuery teacherQuery) {

        Page<EduTeacher> page = new Page<>(current, limit);
        QueryWrapper<EduTeacher> queryWrapper = new QueryWrapper<>();

        String name = teacherQuery.getName();
        Integer level = teacherQuery.getLevel();
        String begin = teacherQuery.getBegin();
        String end = teacherQuery.getEnd();
        if (!StringUtils.isEmpty(name)) {
            queryWrapper.like("name", name);
        }
        if (!StringUtils.isEmpty(level)) {
            queryWrapper.eq("level", level);
        }
        if (!StringUtils.isEmpty(begin)) {
            queryWrapper.ge("gmt_create", begin);
        }
        if (!StringUtils.isEmpty(end)) {
            queryWrapper.le("gmt_create", end);
        }

        IPage<EduTeacher> teacherIPage = teacherService.page(page, queryWrapper);

        long total = teacherIPage.getTotal(); //总记录数
        List<EduTeacher> records = teacherIPage.getRecords(); //数据集合
        return R.ok().data("total", total).data("rows", records);

    }

    //添加讲师
    @ApiOperation(value = "添加讲师")
    @PostMapping("addTeacher")
    public R addTeacher( @RequestBody EduTeacher eduTeacher) {
        boolean b = teacherService.save(eduTeacher);
        if (b) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    //根据讲师id查询
    @ApiOperation(value = "根据讲师id查询")
    @GetMapping("getTeacher/{id}")
    public R addTeacherById (@PathVariable String id){
        EduTeacher byId = teacherService.getById(id);
        return R.ok().data("data", byId);
    }

    //根据讲师id修改
    @ApiOperation(value = "根据讲师id修改")
    @PostMapping("updateTeacherById")
    public R updateTeacherById (@RequestBody EduTeacher eduTeacher){
        boolean b = teacherService.updateById(eduTeacher);
        if (b) {
            return R.ok();
        } else {
            return R.error();
        }
    }

}

